package org.spring.application.processor;

import org.spring.spring.anno.Component;
import org.spring.spring.processor.BeanPostProcessor;
import org.spring.spring.proxy.CglibProxyFactory;

import java.util.Set;

/**
 * @projectName: sky-springboot
 * @package: org.sky.application.processor
 * @className: MyBeanPostProcessor
 * @author: zhl
 * @description: 在bean对象初始化前后对bean对象进行处理
 * 全局增强，有点类似于aop的使用
 * @date: 2024/3/14 14:55
 * @version: 1.0
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * @param beanName :
     * @param object   :
     * @return void
     * @author zhl
     * @description bean对象初始化之前调用
     * @date 2024/3/14 14:53
     */
    @Override
    public Object postProcessorBeforeInitialization(String beanName, Object object) {
        if ("apiService".equals(beanName)){
            System.out.println("对apiService对象方法进行bean对象初始化之前调用");
        }
        return object;
    }

    /**
     * @param beanName :
     * @param object   :
     * @return void
     * @author zhl
     * @description bean对象初始化之后调用
     * @date 2024/3/14 14:53
     */
    @Override
    public Object postProcessorAfterInitialization(String beanName, Object object, Set<String> set) {
        if ("userService".equals(beanName)){
            System.out.println("在userService对象初始化之后调用");
        }
        // 如果已经创建代理对象，则无需再创建
        if (set.isEmpty() || set.contains(beanName)){
            return object;
        }
        return CglibProxyFactory.getProxyInstance(object.getClass(),object);
    }
}
